const path = require('path')
const defualtInfo = require('./src/settings.js')
const TerserPlugin = require('terser-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin')
function resolve(dir) {
    return path.join(__dirname, dir)
}
console.log(process.env.APP_BASE_API)
module.exports = {
    publicPath: './',
    outputDir: process.env.APP_NAME||'dist',
    assetsDir: 'static',
    lintOnSave: false,
    productionSourceMap: false,
    devServer: {
        port: defualtInfo.port,
        open: true,
        overlay: {
            warnings: false,
            errors: true
        },
        proxy: {
            '/api': {
                target: process.env.APP_BASE_API,
                changeOrigin: true,
                pathRewrite: {
                    '^/api':'api'
                }
            },
            //统一鉴权单点登录配置
            '/auth': {
                target: process.env.APP_AUTH_API,
                changeOrigin: true,
                pathRewrite: {
                    '^/auth': 'auth'
                  }
            }
        },
    },

    configureWebpack: (config) =>{
        //网站标题名称
        config.name = defualtInfo.title
        //打包文件大小配置
			config["performance"] = {
				"maxEntrypointSize":10000000,
				"maxAssetSize":30000000
            }
        if(process.env.APP_NAME === 'prod'){ 
            config.mode = 'production'
            config.optimization.minimizer = [
				new TerserPlugin({
                    cache: true,
					 sourceMap: false,
                     parallel: true,
                     terserOptions: {
                     compress: {
                     drop_debugger:true,
                     drop_console:true,
                     },
                    },
				})
			]
            
        }else if(process.env.APP_NAME == 'test'){ 
            config.mode = 'test'
        }else{ 
            config.mode = 'development'
        }
        
        // 开发生产共同配置别名 
        Object.assign(config.resolve,{
            alias: {
                '@': path.resolve(__dirname, './src'),
                'assets': path.resolve(__dirname, './src/assets'),
                'common': path.resolve(__dirname, './src/common'),
                'components': path.resolve(__dirname, './src/components'),
                'network': path.resolve(__dirname, './src/network'),
                'configs': path.resolve(__dirname, './src/configs'),
                'views': path.resolve(__dirname, './src/views'),
                'plugins': path.resolve(__dirname, './src/plugins'),
            }
        }) 
        //开启gzip
        if(process.env.APP_NAME !='dev'){
            return {
                plugins: [new CompressionPlugin({
                  test: /\.(js|css|svg|woff|ttf|json|html|png)$/, //匹配文件名
                  threshold: 10240, //对超过10K的数据进行压缩
                  deleteOriginalAssets: false //是否删除原文件
                })]
              }
        }  
    },
    chainWebpack: (config) =>{
        config.plugins.delete('preload') 
        config.plugins.delete('prefetch')
        //webpack splitChunks
        if(process.env.APP_NAME != 'dev'){
            config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
            // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
            config.optimization.splitChunks({
                chunks: 'all',
                cacheGroups: {
                  libs: {
                    name: 'chunk-libs',
                    test: /[\\/]node_modules[\\/]/,
                    priority: 10,
                    chunks: 'initial' // only package third parties that are initially dependent
                  },
                  elementUI: {
                    name: 'chunk-elementUI', // split elementUI into a single package
                    priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                  },
                  commons: {
                    name: 'chunk-commons',
                    test: resolve('src/components'), // can customize your rules
                    minChunks: 3, //  minimum common number
                    priority: 5,
                    reuseExistingChunk: true
                  }
                }
              })
            config.optimization.runtimeChunk('single')
        }
    }
}